﻿/*-----------------------------------------------
// File: RoundWaitState.cs
// Description: 
// Author: Shaobing	492057342@qq.com
-----------------------------------------------*/
using UnityEngine;
using System.Collections;

namespace Battle.View
{
    public class RoundWaitState : Fsm.FsmStateBase<BattleRoundState>
    {
        public enum RoundWaitPhase
        {
            None,
            Normal,
            WaitForUltimate,
        }
        protected BattleRoundCtrl battleRoundCtrl = null;
        public RoundWaitState(BattleRoundState state, BattleRoundCtrl ctrl) : base(state, ctrl)
        {
            battleRoundCtrl = ctrl;
        }

        protected RoundWaitPhase currentPhase = RoundWaitPhase.None;

        public float UltimateWaitTime
        {
            get { return Dict.Blo.DictConfigBlo.GetRoundWaitUltiTimeOut(); }
        }
        protected float ultimateDeltaTime = 0f;

        protected float autoTimeoutDeltaTime = 0f;


        public override void Enter()
        {
            currentPhase = RoundWaitPhase.Normal;
            BattleViewCtrl.GetInstance().isFirstAttack = true;
            CameraController.Instance.Ready();
            BattleViewCtrl.GetInstance().isUltimateBeginCast = false;
            autoTimeoutDeltaTime = Dict.Blo.DictConfigBlo.GetAutoTimeout();
        }

        protected virtual void CheckGuide()
        {

        }

        public override void Update()
        {
            if (currentPhase == RoundWaitPhase.None)
            {
                currentPhase = RoundWaitPhase.Normal;
            }
            else if (currentPhase == RoundWaitPhase.Normal)
            {
                CheckGuide();

                if (BattleViewCtrl.GetInstance().roundBeginExports.Count > 0)
                {
                    foreach (var msg in BattleViewCtrl.GetInstance().roundBeginExports)
                    {
                        msg.NormalDeal();
                    }
                    BattleViewCtrl.GetInstance().roundBeginExports.Clear();
                }

                if (BattleViewCtrl.GetInstance().isCanBattleOver)
                {
                    BattleManager.Instance.FightOver();
                    return;
                }
                // 自动战斗Tip显示逻辑
                if (BattleViewCtrl.GetInstance().IsHaveAutoFightTimeout && !BattleViewCtrl.GetInstance().hasRoundFight && !BattleViewCtrl.GetInstance().isAuto)
                {
                    autoTimeoutDeltaTime -= Time.deltaTime;
                    BattleManager.Instance.battlePanel.ShowAutoCountDownLabel((int)autoTimeoutDeltaTime);
                    if (autoTimeoutDeltaTime <= 0)
                    {
                        BattleViewCtrl.GetInstance().SendAuto(true);
                    }
                }
                else
                {
                    BattleManager.Instance.battlePanel.HideAutoCountDownLabel();
                }
                // 检测普通技能
                if (BattleViewCtrl.GetInstance().IsHaveBehaviourNeedDo())
                {
                    battleRoundCtrl.SwitchState(BattleRoundState.AttackingState);
                    return;
                }
                // 检测大招
                if (BattleViewCtrl.GetInstance().IsHaveUltimateBehaviour())
                {
                    battleRoundCtrl.SwitchState(BattleRoundState.UltimateBeforeState);
                    return;
                }

                if (IsAllAttacked())
                {
                    if (IsUltimateCanCasted())
                    {
                        ultimateDeltaTime = 0f;
                        currentPhase = RoundWaitPhase.WaitForUltimate;
                        return;
                    }
                    else
                    {
                        battleRoundCtrl.RoundLeave();
                        return;
                    }
                }
            }
            else if (currentPhase == RoundWaitPhase.WaitForUltimate)
            {
                ultimateDeltaTime += Time.deltaTime;

                if ((BattleManager.Instance.battleData.battleType == BattleType.PVE ||
                    BattleManager.Instance.battleData.battleType == BattleType.WANTED ||
                    BattleManager.Instance.battleData.battleType == BattleType.RUSH ||
                    BattleManager.Instance.battleData.battleType == BattleType.RUSH_BOSS ||
                    BattleManager.Instance.battleData.battleType == BattleType.FIELD_PVE ||
                    BattleManager.Instance.battleData.battleType == BattleType.BOSS_RAIDER ||
                    BattleManager.Instance.battleData.battleType == BattleType.FIELD_BOSS ||
                    BattleManager.Instance.battleData.battleType == BattleType.RECRUIT_SHOW)
                    && BattleViewCtrl.GetInstance().curRunningCamp == BattleCamp.Defender)
                {

                }
                else
                {
                    var cooldownnum = Mathf.Clamp(UltimateWaitTime - ultimateDeltaTime, 0f, UltimateWaitTime);
                    BattleManager.Instance.battlePanel.ShowCooldown(cooldownnum);
                }

                if (BattleViewCtrl.GetInstance().isCanBattleOver)
                {
                    BattleManager.Instance.FightOver();
                }

                if (BattleViewCtrl.GetInstance().IsHaveUltimateBehaviour())
                {
                    battleRoundCtrl.SwitchState(BattleRoundState.UltimateBeforeState);
                    return;
                }
                if (ultimateDeltaTime >= UltimateWaitTime)
                {
                    battleRoundCtrl.RoundLeave();
                    return;
                }


            }

        }


        public override void Leave()
        {
            BattleManager.Instance.battlePanel.HideCooldown();
        }



        protected bool IsAllAttacked()
        {
            if (BattleViewCtrl.GetInstance().IsHaveBehaviourNeedDo())
            {
                return false;
            }
            if (BattleViewCtrl.GetInstance().IsHaveUltimateBehaviour())
            {
                return false;
            }
            if (!BattleViewCtrl.GetInstance().isCanRoundOver)
            {
                return false;
            }
            foreach (var uid in BattleViewCtrl.GetInstance().GetActorUids(battleRoundCtrl.currentCamp))
            {
                var bc = BattleViewCtrl.GetInstance().GetBattleCharacter(uid);
                if (!bc.isAlive)
                {
                    continue;
                }
                if (bc != null && bc.GetCurrrentStateType() != ActorState.AttackReady)
                    return false;
            }
            return true;
        }


        /// <summary>
        /// 是否有人可以释放大招
        /// </summary>
        /// <returns></returns>
        protected virtual bool IsUltimateCanCasted()
        {
            // 检查大招释放怒气是否满足条件
            if (BattleViewCtrl.GetInstance().GetAngerNum(battleRoundCtrl.currentCamp) < Dict.Blo.DictConfigBlo.GetUltimateCostAngerNum())
            {
                return false;
            }
            foreach (var uid in BattleViewCtrl.GetInstance().GetActorUids(battleRoundCtrl.currentCamp))
            {
                var bc = BattleViewCtrl.GetInstance().GetBattleCharacter(uid);
                if (!bc.isAlive)
                {
                    continue;
                }
                if (bc.IsUltimateSkillCanCast())
                {
                    return true;
                }
            }
            return false;
        }


    }
}
